package com.xiaoq.social.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.social.entity.FansTagMasterEntity;
import com.xiaoq.social.service.IFansTagMasterService;
import com.xiaoq.social.vo.FansTagMasterVo;
import com.xiaoq.users.redis.RedisTemplate4JedisCluster;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/fanstagmaster")
public class FansTagMasterEndPoint {
	
	@Autowired
	private IFansTagMasterService fansTagMasterSer;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;
	
	private class FansTagMasterListResponse extends GenericListResponse<FansTagMasterVo>{}
	
	private static final Logger LOG = LoggerFactory.getLogger(FansTagMasterEndPoint.class);
	
	@ApiOperation(value = "获取店家标签列表", notes = "获取店家标签列表")
	@ApiResponses({ @ApiResponse(code = 200, response = FansTagMasterListResponse.class, message = "店家标签列表") })
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getFansTagMasterList(HttpServletRequest request){
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		if(StringUtils.isNotEmpty(shopId)){
			List<FansTagMasterEntity> fansTagMasterList = fansTagMasterSer.findFansTagMasterList(shopId);
			List<FansTagMasterVo> fansTagMsterVoList = new ArrayList<>();
			FansTagMasterVo fansTagMasterVo = null;
			for(FansTagMasterEntity fansTagMaster: fansTagMasterList){
				fansTagMasterVo = new FansTagMasterVo();
				BeanUtils.copyProperties(fansTagMaster, fansTagMasterVo);
				fansTagMsterVoList.add(fansTagMasterVo);
				
			}
			return FansTagMasterListResponse.listNoCount(fansTagMsterVoList);
		}else{
			LOG.error("请指明准确的shopId");
		}
		return GenericResponse.ng("查询店家标签列表！");
	}
	
	@ApiOperation(value = "修改标签列表", notes = "修改标签列表)")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "修改结果") })
	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addFansTagMasterList(@RequestParam(value="tags")List<String> tags,HttpServletRequest request){
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		if(StringUtils.isNoneEmpty(shopId)){
			boolean result = fansTagMasterSer.executeFansTagMaster(shopId, tags);
			if(result){
				return GenericResponse.ok();
			}
		}else{
			LOG.error("请指明准确的shopId!");
		}
		return GenericResponse.ng("修改商店标签列表失败！");
	}
	

}
